import { Request, RequestHandler, Response } from 'express';
import {
  IGetSpecialistByIdReq,
  IAddSpecialistReq,
  IUpdateSpecialistReq,
  IDeleteSpecialistReq
} from '../../models/specialist.model';
import * as SpecialistService from '../services/specialist.service';

/**
 * Get active specialist records
 *
 * @param req Express Request
 * @param res Express Response
 */
export const getSpecialists: RequestHandler = async (req: Request, res: Response) => {
  try {
    const specialists = await SpecialistService.getSpecialists();

    res.status(200).json({
      data: specialists,
      code: 2000,
    })
  } catch (error) {
    console.error('[specialists.controller][getSpecialists][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '专家信息列表获取失败！'
    });
  }
};

/**
 * Get specialist record based on id provided
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const getSpecialistById: RequestHandler = async (req: IGetSpecialistByIdReq, res: Response) => {
  try {
    const specialist = await SpecialistService.getSpecialistById(req.params.id);

    res.status(200).json({
      data: specialist,
      code: 2000,
    })
  } catch (error) {
    console.error('[specialists.controller][getSpecialistById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '专家信息获取失败！'
    });
  }
};

/**
 * Inserts a new specialist record
 *
 * @param req Express Request
 * @param res Express Response
 */
export const addSpecialist: RequestHandler = async (req: IAddSpecialistReq, res: Response) => {
  try {
    const result = await SpecialistService.insertSpecialist(req.body);

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error('[specialists.controller][addSpecialist][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    if (error.code === 'ER_DUP_ENTRY') {
        res.status(500).json({
            message: '专家名称重复，请重新输入！'
          });
    } else {
        res.status(500).json({
            message: '专家信息保存失败！'
         });
    }
  }
};

/**
 * Inserts new specialists record
 *
 * @param req Express Request
 * @param res Express Response
 */
export const addSpecialists: RequestHandler = async (req: IAddSpecialistReq, res: Response) => {
  try {
    const specialists = req.body.map((item: any) => [
      item.id,
      item.specialistName, 
      item.specialistSource,
      item.specialistEduBackground,
      item.specialistEduDegree, 
      item.specialistCategory,
      item.specialistSubject,
      item.specialistHonor, 
      item.specialistOrganization,
      item.specialistDirection,
    ])
    const result = await SpecialistService.insertSpecialists(specialists);

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error('[specialists.controller][addSpecialists][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    if (error.code === 'ER_DUP_ENTRY') {
        res.status(500).json({
            message: '专家名称重复，请重新输入！'
          });
    } else {
        res.status(500).json({
            message: '专家信息保存失败！'
         });
    }
  }
};

/**
 * Updates existing specialist record
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const updateSpecialistById: RequestHandler = async (req: IUpdateSpecialistReq, res: Response) => {
  try {
    const result = await SpecialistService.updateSpecialist({...req.body, id: req.params.id});

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error('[specialists.controller][updateSpecialistById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    if (error.code === 'ER_DUP_ENTRY') {
        res.status(500).json({
            message: '专家名称重复，请重新输入！'
          });
    } else {
        res.status(500).json({
            message: '专家信息更新失败！'
         });
    }
  }
};

/**
 * deletes a specialist
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const deleteSpecialistById: RequestHandler = async (req: IDeleteSpecialistReq, res: Response) => {
  try {
    const result = await SpecialistService.deleteSpecialist(req.params.id);

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error('[specialists.controller][deleteSpecialistById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '专家信息删除失败！'
    });
  }
};